﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.BLL.Core.Model;
using System.Data;
using InformationSystem.Utility;
using InformationSystem.BLL.Core.Dao;


namespace InformationSystem.BLL.Core.Service.Impl
{
    public class StudentServiceImpl : IStudentService
    {
        public ICollegeDao CollegeDao { get; set; }
        public IStudentDao StudentDao { get; set; }

        public event EventHandler OnupdateProgress;
        public event EventHandler Complete;

        public bool UpdateStudentBasicInfo(StudentInfo studentinfo)
        {
            throw new NotImplementedException();
        }

        public bool UpdateStudentCourse()
        {
            throw new NotImplementedException();
        }

        public bool DeleteStudentCourse()
        {
            throw new NotImplementedException();
        }

        public bool DeleteStudent()
        {
            throw new NotImplementedException();
        }

        //public IList<StudentMark> GetStudentMarkList(LoginUser user)
        //{
        //    return StudentDao.GetStudentMarkList(user);
        //}


        public bool ImportStudentMark(DataTable sourcedt, int coursesession)
        {
            //处理流程
            //1、移除 加权平均 行
            if (sourcedt.Columns.Contains(Common.Average))
                sourcedt.Columns.Remove(Common.Average);
            //查询课程号所在列数
            int courseidindex = sourcedt.Columns.IndexOf(Common.CourseID);
            for (int i = courseidindex + 1; i < sourcedt.Columns.Count; i++)
            {
                //创建课程信息
                Course course = new Course();
                course.CourseID = sourcedt.Columns[i].ColumnName;
                course.CourseName = sourcedt.Rows[0][i].ToString();
                course.CourseWeight = sourcedt.Rows[1][i].ToString();
                StudentDao.CreateCourse(course);
            }
            sourcedt.Columns[0].ColumnName = Common.StuID;
            //以0计数 ，从第3行处理，判断有无 学号 姓名
            List<StudentMark> stumlist = new List<StudentMark>();
            int index = 0;
            foreach (DataRow item in sourcedt.Rows)
            {
                index++;
                if (item[Common.StuID].ToString().Trim() == string.Empty || item[Common.StuID].ToString().Trim() == Common.StuID) continue;
                stumlist.Clear();
                for (int i = courseidindex + 1; i < sourcedt.Columns.Count; i++)
                {
                    //没有此门课，继续
                    if (item[i].ToString() == string.Empty) continue;
                    StudentMark mark = new StudentMark();
                    mark.UserID = item[Common.StuID].ToString().Trim();
                    mark.CourseID = sourcedt.Columns[i].ColumnName;
                    mark.CourseSession = coursesession;
                    mark.MarkString = item[i].ToString();
                    stumlist.Add(mark);
                }
                bool result = StudentDao.ImportStudentMark(stumlist);
                if (this.OnupdateProgress != null)
                {
                    MyEventArgs myarg = new MyEventArgs();
                    myarg.Current = index;
                    myarg.HasMsg = !result;
                    myarg.Msg = string.Format("学号 {0} 部分成绩导入失败，请检查数据！", item[Common.StuID].ToString());
                    this.OnupdateProgress(this, myarg);
                }
            }
            if (this.Complete != null)
                this.Complete(this, new EventArgs());
            return true;
        }

        public bool ImportStudentInfo(DataTable sourcedt, LoginUser user)
        {
            List<string> classList = new List<string>();
            StudentInfo sinfo = null;
            int index = 0;
            foreach (DataRow item in sourcedt.Rows)
            {
                index++;
                sinfo = new StudentInfo();
                sinfo.UserID = item[Common.StuID].ToString().Trim();
                sinfo.UserName = item[Common.Name].ToString().Trim();
                sinfo.ClassID = item[Common.Class].ToString().Trim();
                sinfo.CollegeID = user.CollegeID;
                sinfo.GradeID = user.GradeID;
                sinfo.LoginPwd = MSecurity.Encrypt(sinfo.UserID);
                sinfo.Sex = item[Common.Sex].ToString().Trim();
                sinfo.MarjorName = item[Common.MajorName].ToString().Trim();
                if (!this.Check(sinfo))
                {
                    if (this.OnupdateProgress != null)
                    {
                        MyEventArgs myarg = new MyEventArgs();
                        myarg.Current = index;
                        myarg.HasMsg = true;
                        myarg.Msg = string.Format("第{0}条记录信息不完整，拒绝导入。", index);
                        this.OnupdateProgress(this, myarg);
                    }
                    continue;
                }
                if (!classList.Contains(sinfo.ClassID))
                {
                    //检查班级信息。不存在的则创建
                    ClassInfo classinfo = new ClassInfo();
                    classinfo.ClassID = sinfo.ClassID;
                    classinfo.ClassName = sinfo.MarjorName;
                    classinfo.CreateDateTime = DateTime.Now;
                    classinfo.CreateUserID = user.UserID;
                    classinfo.GradeID = user.GradeID;
                    classinfo.CollegeID = user.CollegeID;
                    CollegeDao.CreateClassInfo(classinfo, user);
                    classList.Add(sinfo.ClassID);
                }
                bool result = StudentDao.ImportStudentInfo(sinfo);
                if (this.OnupdateProgress != null)
                {
                    MyEventArgs myarg = new MyEventArgs();
                    myarg.Current = index;
                    myarg.HasMsg = !result;
                    myarg.Msg = string.Format("第{0}条记录错误：{1}.", index, sinfo.Msg);
                    this.OnupdateProgress(this, myarg);
                }
            }
            if (this.Complete != null)
                this.Complete(this, new EventArgs());
            return true;
        }

        private bool Check(StudentInfo stu)
        {
            if (stu.ClassID == string.Empty || stu.UserID == string.Empty || stu.UserName == string.Empty || stu.MarjorName == string.Empty)
                return false;
            return true;
        }
             


        public bool CreateCourse(Course course)
        {
            return StudentDao.CreateCourse(course);
        }



        public IList<StudentMark> GetStudentMarkList(string studentid, LoginUser user)
        {
            //user用以权限控制，先可以不实现            
            return StudentDao.GetStudentMarkList(studentid);
        }

        public StudentInfo GetStudentInfo(string studentid, LoginUser user)
        {
            return StudentDao.GetStudentInfo(studentid);
        }

        public IList<StudentInfo> GetStudentList(string classid, LoginUser user)
        {

            return StudentDao.GetStudentList(classid);
        }
    }
}
